import unittest

from parameterized import parameterized
from common.read_json_data import build_data
from config import BASE_DIR
from api.login_api import LoginApi
import logging
from common.verify_result import common_assert


class TestLogin(unittest.TestCase):
    @parameterized.expand(build_data(BASE_DIR + '/data/login.json'))
    def test_login(self, case_name, body, expect_result):
        print(f'【{case_name}】接口测试')
        logging.info(f'【{case_name}】接口测试')
        print(f'body = {body}')
        logging.info(f'body = {body}')
        print(f'expect_result = {expect_result}')
        logging.info(f'expect_result = {expect_result}')

        # 1.调用api接口，发送登陆请求，获取响应对象
        # 1.1 实例化对象
        login_obj = LoginApi()
        # 2.实例化对象调用实例方法，发送请求，获取响应对象
        resp = login_obj.login(body)
        # 3.打印响应内容的json格式
        json_data = resp.json()
        print(f'json_data = {json_data}')
        logging.info(f'json_data = {json_data}')
        # 4.断言expect_result预期结果的字典格式
        # self.assertEqual(expect_result['status_code'], resp.status_code)
        # self.assertEqual(expect_result['success'], json_data['success'])
        # self.assertEqual(expect_result['code'], json_data['code'])
        # self.assertIn(expect_result['message'], json_data['message'])

        # 调用断言函数
        common_assert(self, expect_result, resp)
